package com.ruoyi.service.service.impl;

import com.ruoyi.service.domain.ServiceFlag;
import com.ruoyi.service.domain.ServiceRule;
import com.ruoyi.service.entity.rule.RuleVO;
import com.ruoyi.service.mapper.ServiceFlagMapper;
import com.ruoyi.service.mapper.ServiceRuleMapper;
import com.ruoyi.service.service.IServiceRuleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static com.ruoyi.common.utils.SecurityUtils.getUsername;

/**
 * 接单退单规则Service业务层处理
 * 
 * @author ZDL
 * @date 2025-03-10
 */
@Service
public class ServiceRuleServiceImpl implements IServiceRuleService 
{
    @Autowired
    private ServiceRuleMapper serviceRuleMapper;
    @Autowired
    private ServiceFlagMapper serviceFlagMapper;

    /**
     * 查询接单退单规则
     * 
     * @param ruleId 接单退单规则主键
     * @return 接单退单规则
     */
    @Override
    public RuleVO selectServiceRuleByRuleId(Long ruleId)
    {
        ServiceRule rule = serviceRuleMapper.selectServiceRuleByRuleId(ruleId);

        RuleVO ruleVO = new RuleVO();
        ruleVO.init(rule);
        if (rule.getRuleFlag() != null && "1".equals(rule.getRuleType())){
            ServiceFlag serviceFlag = serviceFlagMapper.selectServiceFlagByFlagCode(String.valueOf(rule.getRuleFlag()));
            if (serviceFlag != null){
                ruleVO.setRuleFlag(serviceFlag.getFlagDesc());
            }
        }

        return ruleVO;
    }

    /**
     * 查询接单退单规则列表
     * 
     * @param serviceRule 接单退单规则
     * @return 接单退单规则
     */
    @Override
    public List<RuleVO> selectServiceRuleList(ServiceRule serviceRule)
    {
        List<RuleVO> ruleVOList = new ArrayList<RuleVO>();
        serviceRule.setRuleDelFlag("0");
        List<ServiceRule> serviceRules = serviceRuleMapper.selectServiceRuleList(serviceRule);
        for (ServiceRule rule : serviceRules) {
            RuleVO ruleVO = new RuleVO();
            ruleVO.init(rule);
            if (rule.getRuleFlag() != null && "1".equals(rule.getRuleType())){
                ServiceFlag serviceFlag = serviceFlagMapper.selectServiceFlagByFlagCode(String.valueOf(rule.getRuleFlag()));
                if (serviceFlag != null){
                    ruleVO.setRuleFlag(serviceFlag.getFlagDesc());
                }
            }
            ruleVOList.add(ruleVO);
        }
        return ruleVOList;
    }

    /**
     * 查询接单退单规则列表
     *
     * @param serviceRule 接单退单规则
     * @return 接单退单规则
     */
    @Override
    public List<ServiceRule> selectServiceRuleListOld(ServiceRule serviceRule)
    {

        return serviceRuleMapper.selectServiceRuleList(serviceRule);
    }

    /**
     * 新增接单退单规则
     * 
     * @param serviceRule 接单退单规则
     * @return 结果
     */
    @Override
    public int insertServiceRule(ServiceRule serviceRule)
    {
        serviceRule.setRuleCreateBy(getUsername());
        serviceRule.setRuleUpdateBy(getUsername());
        serviceRule.setRuleCreateTime(new Date());
        serviceRule.setRuleUpdateTime(new Date());
        return serviceRuleMapper.insertServiceRule(serviceRule);
    }

    /**
     * 修改接单退单规则
     * 
     * @param rule 接单退单规则
     * @return 结果
     */
    @Override
    public int updateServiceRule(RuleVO rule)
    {
        ServiceRule serviceRule = new ServiceRule();
        serviceRule.init(rule);
        ServiceFlag serviceFlag = serviceFlagMapper.selectServiceFlagByFlagDesc(String.valueOf(rule.getRuleFlag()));
        if (serviceFlag != null){
            serviceRule.setRuleFlag(Integer.valueOf(serviceFlag.getFlagCode()));
        }else{
            serviceRule.setRuleFlag(Integer.valueOf(rule.getRuleFlag()));
        }
        serviceRule.setRuleCreateBy(getUsername());
        serviceRule.setRuleUpdateBy(getUsername());
        serviceRule.setRuleCreateTime(new Date());
        serviceRule.setRuleUpdateTime(new Date());
        return serviceRuleMapper.updateServiceRule(serviceRule);
    }

    /**
     * 批量删除接单退单规则
     * 
     * @param ruleIds 需要删除的接单退单规则主键
     * @return 结果
     */
    @Override
    public int deleteServiceRuleByRuleIds(Long[] ruleIds)
    {
        return serviceRuleMapper.deleteServiceRuleByRuleIds(ruleIds);
    }

    /**
     * 删除接单退单规则信息
     * 
     * @param ruleId 接单退单规则主键
     * @return 结果
     */
    @Override
    public int deleteServiceRuleByRuleId(Long ruleId)
    {
        return serviceRuleMapper.deleteServiceRuleByRuleId(ruleId);
    }
}
